package com.qdlc.p2p.biz.core.tender;

import java.util.List;

import com.qdlc.p2p.common.core.ReturnResult;
import com.qdlc.p2p.common.exception.BusinessException;
import com.qdlc.p2p.dal.dto.TppTradeLog;
import com.qdlc.p2p.dal.dto.User;
import com.qdlc.p2p.dal.model.BorrowModel;

/**
 * 策略执行上下文
 *
 * @author wuaj
 */
public class BorrowStrategyContext {

    private IBorrowStrategy strategy;

    public BorrowStrategyContext(IBorrowStrategy strategy) {
        this.strategy = strategy;
    }

    public void tender(BorrowModel borrowModel, User tenderUser) {
        // 生成订单
        TppTradeLog tppTradeLog = strategy.createTenderOrder(borrowModel, tenderUser);
        // 投标
        if (tppTradeLog != null) {
            ReturnResult returnResult = strategy.tender(tppTradeLog, tenderUser);
            if (returnResult.isError()) {
                throw new BusinessException(returnResult.getErrorInfo(), BusinessException.TYPE_JSON);
            }
        } else {
            strategy.tender(borrowModel, tenderUser);
        }
    }

    /**
     * 满标复审-通过
     *
     * @author Kaiju Zhao
     * @param borrowModel 借款标
     */
    public void verifyFullSuccess(BorrowModel borrowModel) {
        // 生成订单
        TppTradeLog tppTradeLog = strategy.createLoanPayOrder(borrowModel);
		ReturnResult returnResult = strategy.verifyFullSuccess(tppTradeLog);
		if (returnResult.isError()) {
			throw new BusinessException(returnResult.getErrorInfo(), BusinessException.TYPE_JSON);
		}
    }

    /**
     * 满标复审-不通过
     *
     * @author Kaiju Zhao
     * @param borrowModel 借款标
     */
    public void verifyFullFail(BorrowModel borrowModel) {
        // 生成订单
        List<TppTradeLog> tppTradeLogList = strategy.createRefundCollectOrder(borrowModel);
		ReturnResult returnResult = strategy.verifyFullFail(tppTradeLogList);
		if (returnResult.isError()) {
			throw new BusinessException(returnResult.getErrorInfo(), BusinessException.TYPE_JSON);
		}
    }

    /**
     * 撤回标
     *
     * @author Kaiju Zhao
     * @param borrowModel 借款标
     */
    public void cancelBorrow(BorrowModel borrowModel) {
        // 生成订单
        List<TppTradeLog> tppTradeLogList = strategy.createRefundCollectOrder(borrowModel);
        ReturnResult returnResult = strategy.cancelBorrow(tppTradeLogList);
        if (returnResult.isError()) {
            throw new BusinessException(returnResult.getErrorInfo(), BusinessException.TYPE_JSON);
        }
    }


}
